<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\BaseController;
use App\Http\Events\Admin\AdminEvent;
use App\Http\Model\Admin\Role;
use Config;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class RoleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $role = new Role();
        $role_data = $role->findAllByPaginate();
        return view('admin.role', ['role_list' => $role_data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.role_create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = Input::except('_token');

        $validator = $this->formValidator($input, Config::get('admin.role.rules'), Config::get('admin.role.message'));
        if ($validator->passes()) {
            $re = (new Role())->createNew($input);
            if ($re) {
                event(new AdminEvent('create_role', $re));
                return redirect('admin/role');
            } else {
                return back()->with('errors', Config::get('admin.errors.createFail'));
            }
        } else {
            return back()->withErrors($validator);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $role = new Role();
        $one = $role->findOneById($id);
        return view('admin.role_edit', ['role_info' => $one]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = Input::except('_token', '_method');

        $validator = $this->formValidator($input, Config::get('admin.role.rules'), Config::get('admin.role.message'));
        if ($validator->passes()) {
            $old_user = (new Role())->findOneById($id)->toArray();
            $re = (new Role())->updateOneById($id, $input);
            if ($re) {
                event(new AdminEvent('edit_role', $input, $old_user));
                return redirect('admin/role');
            } else {
                return back()->with('errors', Config::get('admin.errors.editFail'));
            }
        } else {
            return back()->withErrors($validator);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $old_obj = (new Role())->findOneById($id);
        $rs = (new Role())->deleteById($id);
        if($rs){
            event(new AdminEvent('remove_role',$old_obj,''));
            return redirect('admin/role');
        }
    }
}
